package cn.yunhe.controller;

import cn.yunhe.entity.Order;
import cn.yunhe.entity.User;
import cn.yunhe.service.UserService;
import cn.yunhe.util.Result;
import cn.yunhe.util.UUIDGenerator;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.yunhe.enums.StatusCode.Invalid_Param;

/**
 * @author 孙敖
 * @date 2018/9/14 19:53
 */

@RestController
/**
 * 返回的是基本数据类型和String，那么就返回基本数据类型和字符串 如果返回的是 对象，List,Map 等，将会自动转换为json格式
 * 此时，如果依然想返回某个视图，可以使用ModelAndView
 */

@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 查询所有的客户信息,跳转页面
     * @return
     */
    @RequestMapping("/toList")
    public ModelAndView toList(){
        return new ModelAndView("user_list");
    }

    /**
     * 查询所有黑名单客户信息,跳转页面
     * @return
     */
    @RequestMapping("/toBlackList")
    public ModelAndView toBlackList(){
        return new ModelAndView("user_blackList");
    }

    @RequestMapping("/user_order")
    public Set<Order> searchOrders(User user){
        User user1=userService.findUserOrder(user);
        return user1.getOrders();
    }


    //增加客户控制器
    @RequestMapping("/user_add")
    public Result addUser(User user){

        user.setSuId(UUIDGenerator.getUUID());
        userService.addUser(user);
        
        return new Result();
    }
    //添加用户跳转新的页面
    @RequestMapping("/add_User")
    public ModelAndView add_User(){

        return new ModelAndView("addUser");
    }

    //获取客户ID
    @RequestMapping("/user_toEdit")
    public ModelAndView toEdit(User user){
        User u=userService.getUserById(user);
        Map map=new HashMap();
        map.put("data",u);
        return new ModelAndView("user_edit",map);
    }

    //修改用户信息
    @RequestMapping("/user_edit")
    public Result editUser(User user){
        userService.updateUser(user);
        return new Result();
    }

    //更新客户状态
    @RequestMapping("/user_upUserStatus")
    public Result upUserStatus(User user){
        //也可在Mapper中定义,使s_status =#{1}
        System.out.println(user.getSvip());

        userService.upUserStatus(user);
        return new Result();
    }

    //更新黑名单客户状态
    @RequestMapping("/user_upBlackStatus")
    public Result upBlackStatus(User user){

        //也可在Mapper中定义,使s_status =#{1}
        System.out.println(user.getSuId());
        userService.upBlackStatus(user);
        return new Result();
    }

    /**
     * 查询用户集合,分页
     * @param pageSize   页大小
     * @param pageNum    总页数
     * @param keyword       用户
     * @return
     */
    @RequestMapping("/getPageUser")
    public Map queryUserByPage(int pageSize,int pageNum,String keyword){
        Map map=new HashMap();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("keyword",keyword);
        Page page=userService.queryLikeUsers(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return map;
    }

    /**
     * 查询黑名单用户集合,分页
     * @param pageSize   页大小
     * @param pageNum   总页数
     * @param keyword  用户
     * @return
     */
    @RequestMapping("/getPageBlackList")
    public Map pageBlackList(int pageSize,int pageNum,String keyword){
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("keyword",keyword);
        Page page=userService.getLikeBlackList(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return map;
    }

    /**
     * Service访问器
     * @return
     */
    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
